package com.greate.community.controller;

import com.greate.community.entity.FraudCase;
import com.greate.community.service.FraudCaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;

/**
 * 诈骗案例控制器
 */
@Slf4j
@Controller
@RequestMapping("/fraud")
public class FraudCaseController {

    @Autowired
    private FraudCaseService fraudCaseService;

    /**
     * 获取案例列表
     */
    @GetMapping("/list")
    public String getCaseList(Model model) {
        log.info("获取诈骗案例列表");
        try {
            List<FraudCase> cases = fraudCaseService.findAllCases();
            model.addAttribute("fraudCases", cases);
            return "site/fraud-list";
        } catch (Exception e) {
            log.error("获取案例列表失败", e);
            throw e;
        }
    }

    /**
     * 获取案例详情
     */
    @GetMapping("/detail/{id}")
    public String getCaseDetail(@PathVariable("id") int id, Model model) {
        FraudCase fraudCase = fraudCaseService.findCaseById(id);
        model.addAttribute("fraudCase", fraudCase);
        return "site/fraud-detail";
    }

    /**
     * 按类型获取案例
     */
    @GetMapping("/type")
    public String getCasesByType(@RequestParam("type") String type, Model model) {
        log.info("按类型获取诈骗案例，类型：{}", type);
        try {
            List<FraudCase> cases = fraudCaseService.findCasesByType(type);
            log.info("成功获取类型为{}的案例，数量：{}", type, cases.size());
            model.addAttribute("fraudCases", cases);
            model.addAttribute("currentType", type);
            return "site/fraud-list";
        } catch (Exception e) {
            log.error("按类型获取案例失败，类型：{}", type, e);
            throw e;
        }
    }

    /**
     * 添加诈骗案例
     */
    /**
     * 跳转到新增案例页面
     */
    @GetMapping("/publish")
    public String getPublishPage() {
        return "site/fraud-publish";
    }

    @PostMapping("/add")
    public String addCase(@RequestParam @NotEmpty(message = "文章标题不能为空") String title, 
                         @RequestParam String content, 
                         @RequestParam @NotEmpty(message = "案例类型不能为空") String type) {

        FraudCase fraudCase = new FraudCase();
        fraudCase.setTitle(title);
        fraudCase.setContent(content);
        fraudCase.setType(type);
        fraudCase.setStatus(0);
        fraudCase.setCreateTime(new Date());
        fraudCase.setUpdateTime(new Date());
        
        try {
            log.info("添加诈骗案例，标题：{}，类型：{}", title, type);
            fraudCaseService.addCase(fraudCase);
            log.info("成功添加诈骗案例，ID：{}", fraudCase.getId());
            return "redirect:/fraud/list";
        } catch (Exception e) {
            log.error("添加诈骗案例失败，标题：{}，类型：{}", title, type, e);
            return "redirect:/fraud/publish";
        }
    }
}